/* typehints:start */
import { GameRoot } from "../../root";
/* typehints:end */

import { ClickDetector } from "../../core/utils/click_detector";

export class BaseHUDPart{
  constructor(root){
    /** @type {GameRoot} */
    this.root = root;

    this.clickDetectors = [];
  }

  /**
   * 
   * @param {HTMLElement} parent 
   */
  createElements(parent){}

  initialize(){
    abstract;
  }

  /**
   * 更新内容及逻辑
   */
  update(){}

  /**
   * 长按或点击元素
   * @param {*} element 
   * @param {*} longPress 
   * @param {*} click 
   * @param {*} args 
   */
  trackLongPressAndClick(element, longPress, click, args = {}){
    const detector = new ClickDetector(element, {
      longPressMs: 600,
      ...args,
    });
    detector.longPress.add(longPress, this);
    detector.click.add(click, this);
    this.clickDetectors.push(detector);
    if(G_IS_DEV){
      detector._src = 'hud-' + this.constructor.name;
    }
  }

  /**
   * 点击元素
   */
  trackClicks(element, handler, args = {}){
    const detector = new ClickDetector(element, args);
    detector.click.add(handler, this);
    this.clickDetectors.push(detector);
    if(G_IS_DEV){
      detector._src = 'hud-' + this.constructor.name;
    }
  }

  /**
   * 长按元素
   */
  trackLongPress(element, handler, args = {}){
    const detector = new ClickDetector(element, {
      longPressMs: 600,
      ...args,
    });
    detector.longPress.add(handler, this);
    this.clickDetectors.push(detector);
    if(G_IS_DEV){
      detector._src = 'hud-' + this.constructor.name;
    }
  }

  cleanup(){
    this.cleanupClickDetectors();
  }

  cleanupClickDetectors(){
    if(this.clickDetectors){
      for(let i = 0; i < this.clickDetectors.length; i++){
        this.clickDetectors[i].cleanup();
      }
      this.clickDetectors = [];
    }
  }
}